/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

package ace.states;

import ace.component.Button;
import ace.component.ButtonPush;
import ace.component.ButtonWheel;
import ace.game.entity.body.BodyData;
import ace.game.entity.body.BodyFactory;
import ace.game.entity.player.Player;
import ace.game.entity.player.Profile;
import ace.game.entity.weapon.Weapon;
import ace.game.entity.weapon.WeaponData;
import ace.game.entity.weapon.WeaponFactory;
import ace.system.BitmapFont;
import ace.system.StateManager;
import ace.system.Utilities;
import ace.system.sound.SFXPlayer;
import ace.system.sound.SimplePlayer;
import java.io.IOException;
import java.util.Vector;
import javax.microedition.lcdui.Graphics;
import javax.microedition.lcdui.Image;
import javax.microedition.lcdui.game.GameCanvas;

/**
 *
 * @author TimotiusNC
 */
public class StateGarage extends GameState {
    // TODO: persis to rms
	public StateGarage(Vector param){
		super(GameState.GARAGE_MENU, StateManager.instance().getBackgroundImage());

		m_Profile   = (Profile) param.elementAt(0);
        m_State     = STATE_GARAGE;
		m_ConfirmIdx = 0;
		m_IndexSave	= new int[14];
		for(int i=0; i<10; ++i){
			m_IndexSave[i] = -1;
		}

        // construct arrays
        m_BuyableBody           = new Vector();
        m_BuyableWeapon         = new Vector();
        m_SellableBody          = new Vector();
        m_SellableWeapon        = new Vector();
        m_AssembleableBody      = new Vector();
        m_AssembleableWeapon    = new Vector();

		//Confirmation panel and its buttons
		m_ConfirmMessage = BitmapFont.getFont(BitmapFont.MAIN).renderTransparentText("Are you sure?", 0x000000);
		try {
			m_ConfirmPanel = Image.createImage(Utilities.INTERFACE_FOLDER + "/pause.png");
		} catch (IOException ex) {}

		m_BtnConfirm = new Vector();
		for(int i=0; i<m_ConfirmationString.length; ++i){
			ButtonPush bp = new ButtonPush(m_ConfirmationString[i], i, 90, 120 + i*25);
			bp.setAlignment(Graphics.HCENTER | Graphics.VCENTER);
			bp.setPointerAlignment(Graphics.HCENTER | Graphics.VCENTER);
			m_BtnConfirm.addElement(bp);
		}
		((ButtonPush)m_BtnConfirm.elementAt(m_ConfirmIdx)).setState(Button.STATE_SELECTED);

		StateManager.instance().getSimplePlayer().setSong(Utilities.BGM_FOLDER + "/garage.mid", "audio/mid", SimplePlayer.LOOP_INF);
		StateManager.instance().getSimplePlayer().startPlayer();

		initPlayerPosition();
		initComp(m_GarageMenuString);
	}

	private void initPlayerPosition(){
		m_Player = m_Profile.buildPlayer();
		m_Player.position.x = 93;
		m_Player.position.y = StateManager.instance().getHeight() / 2 + 1;

        // placeholder in buy, sell and assembly
        m_CurrentPlayer = m_Profile.buildPlayer();
        m_CurrentPlayer.position.x = m_Player.position.x;
        m_CurrentPlayer.position.y = m_Player.position.y;
	}

	private void initComp(String[] menuStrings){
		if(m_Menu == null || m_IndexSave[m_State]==-1)	m_Index = 0;
		else											m_Index = m_IndexSave[m_State];

		ButtonPush bp	= null;
		if(menuStrings.length > 1){
			m_Menu = new ButtonWheel(menuStrings.length <=4 ? 3 : (menuStrings.length+1) / 2, 0, "/selection.png", null);
			for(int i=0; i<menuStrings.length; ++i){
				bp = new ButtonPush(menuStrings[i], BitmapFont.BITSUMISHI_BLUE, i, 0, 0);
				bp.setAlignment(Graphics.RIGHT | Graphics.TOP);
				m_Menu.addButton(i, bp);
			}

			bp = new ButtonPush("Back", BitmapFont.BITSUMISHI_BLUE, menuStrings.length, 0, 0);
			bp.setAlignment(Graphics.RIGHT | Graphics.TOP);
			m_Menu.addButton(menuStrings.length, bp);
		} else{
			if(menuStrings.length <= 0){
				menuStrings = new String[1];
				menuStrings[0] = "Back";
			}

			if(menuStrings[0].equals("Back"))	m_Menu = new ButtonWheel(1, 0, "/selection.png", null);
			else								m_Menu = new ButtonWheel(2, 0, "/selection.png", null);
			
			for(int i=0; i<menuStrings.length; ++i){
				bp = new ButtonPush(menuStrings[i], BitmapFont.BITSUMISHI_BLUE, i, StateManager.instance().getWidth(), 100 + i*20);
				bp.setAlignment(Graphics.RIGHT | Graphics.TOP);
				m_Menu.addButton(i, bp);
			}

			if(!menuStrings[0].equals("Back")){
				bp = new ButtonPush("Back", BitmapFont.BITSUMISHI_BLUE, menuStrings.length, StateManager.instance().getWidth(), 100 + menuStrings.length*20);
				bp.setAlignment(Graphics.RIGHT | Graphics.TOP);
				m_Menu.addButton(menuStrings.length, bp);
			}

			m_Menu.setSelection1Offset(0, -5);
			m_Menu.setImmediate(false);
		}

		m_Menu.setPosition(StateManager.instance().getWidth(), 93);
		m_Menu.setSelection1Align(Graphics.RIGHT | Graphics.TOP);
		m_Menu.setSelectedID(m_Index); //Defaulted it to BACK button
		
        m_MoneyStringImage = BitmapFont.getFont(BitmapFont.BITSUMISHI_BLUE).renderTransparentText(m_Profile.getMoney() + " DP", 0x000000);
	}

    private void initBuyBody() {
        boolean[] boughtBody    = m_Profile.getBoughtBody();
        boolean[] buyableBody   = m_Profile.getBuyableBody();
        m_BuyableBody.removeAllElements();

        for (int i = 0; i < buyableBody.length; ++i) {
            if (buyableBody[i] && !boughtBody[i]) {
                m_BuyableBody.addElement(BodyFactory.instance().getBodyData(i));
            }
        }

        String[] soldBodyString = new String[m_BuyableBody.size()];
        for (int i = 0; i < soldBodyString.length; ++i) {
            soldBodyString[i] = ((BodyData)m_BuyableBody.elementAt(i)).getName();
        }
        initComp(soldBodyString);
    }

	private void confirmBodyPurchase(BodyData data){
		if (m_Profile.getMoney() >= data.getPrice()) {
			StateManager.instance().getSFXPlayer().playSFX(SFXPlayer.SFX_BUILD);
			m_Profile.addMoney(-data.getPrice());
			m_Profile.getBoughtBody()[data.getIndex()] = true;
			initBuyBody();
		} else { //play error sfx
            StateManager.instance().getSFXPlayer().playSFX(SFXPlayer.SFX_ERROR);
		}
	}

    private void initBuyWeapon() {
        boolean[] boughtWeapon  = m_Profile.getBoughtWeapon();
        boolean[] buyableWeapon = m_Profile.getBuyableWeapon();
        m_BuyableWeapon.removeAllElements();

        for (int i = 0; i < buyableWeapon.length; ++i) {
            if (buyableWeapon[i] && !boughtWeapon[i]) {
                m_BuyableWeapon.addElement(WeaponFactory.instance().getWeaponData(i));
            }
        }

        String[] soldWeaponString = new String[m_BuyableWeapon.size()];
        for (int i = 0; i < soldWeaponString.length; ++i) {
            soldWeaponString[i] = ((WeaponData)m_BuyableWeapon.elementAt(i)).getName();
        }
        initComp(soldWeaponString);
    }

	private void confirmWeaponPurchase(WeaponData data){
		if (m_Profile.getMoney() >= data.getPrice()) {
			StateManager.instance().getSFXPlayer().playSFX(SFXPlayer.SFX_BUILD);
			m_Profile.addMoney(-data.getPrice());
			m_Profile.getBoughtWeapon()[data.getIndex()] = true;
			initBuyWeapon();
		} else { //play error sfx
            StateManager.instance().getSFXPlayer().playSFX(SFXPlayer.SFX_ERROR);
		}
	}

    private void initSellBody() {
        boolean[] boughtBody    = m_Profile.getBoughtBody();
        boolean[] buyableBody   = m_Profile.getBuyableBody();
        m_SellableBody.removeAllElements();

        for (int i = 2; i < buyableBody.length; ++i) {
            if (buyableBody[i] && boughtBody[i]) {
                m_SellableBody.addElement(BodyFactory.instance().getBodyData(i));
            }
        }

        String[] soldBodyString = new String[m_SellableBody.size()];
        for (int i = 0; i < soldBodyString.length; ++i) {
            soldBodyString[i] = ((BodyData)m_SellableBody.elementAt(i)).getName();
        }
        initComp(soldBodyString);
    }

	private void confirmBodySelling(BodyData data){
		StateManager.instance().getSFXPlayer().playSFX(SFXPlayer.SFX_BUILD);
		m_Profile.addMoney(data.getPrice());
		m_Profile.getBoughtBody()[data.getIndex()] = false;
		initSellBody();
	}

    private void initSellWeapon() {
        boolean[] boughtWeapon  = m_Profile.getBoughtWeapon();
        boolean[] buyableWeapon = m_Profile.getBuyableWeapon();
        m_SellableWeapon.removeAllElements();

        for (int i = 2; i < buyableWeapon.length; ++i) {
            if (buyableWeapon[i] && boughtWeapon[i]) {
                m_SellableWeapon.addElement(WeaponFactory.instance().getWeaponData(i));
            }
        }

        String[] soldWeaponString = new String[m_SellableWeapon.size()];
        for (int i = 0; i < soldWeaponString.length; ++i) {
            soldWeaponString[i] = ((WeaponData)m_SellableWeapon.elementAt(i)).getName();
        }
        initComp(soldWeaponString);
    }

	private void confirmWeaponSelling(WeaponData data){
		StateManager.instance().getSFXPlayer().playSFX(SFXPlayer.SFX_BUILD);
		m_Profile.addMoney(data.getPrice());
		m_Profile.getBoughtWeapon()[data.getIndex()] = false;
		initSellWeapon();
	}

    private void initAssemblyBody() {
        boolean[] boughtBody    = m_Profile.getBoughtBody();
        boolean[] buyableBody   = m_Profile.getBuyableBody();
        m_AssembleableBody.removeAllElements();

        for (int i = 0; i < buyableBody.length; ++i) {
            if (boughtBody[i]) {
                m_AssembleableBody.addElement(BodyFactory.instance().getBodyData(i));
            }
        }

        String[] soldBodyString = new String[m_AssembleableBody.size()];
        for (int i = 0; i < soldBodyString.length; ++i) {
            soldBodyString[i] = ((BodyData)m_AssembleableBody.elementAt(i)).getName();
        }
        initComp(soldBodyString);
    }

    private void initAssemblyWeapon() {
        boolean[] boughtWeapon  = m_Profile.getBoughtWeapon();
        boolean[] buyableWeapon = m_Profile.getBuyableWeapon();
        m_AssembleableWeapon.removeAllElements();

        for (int i = 0; i < buyableWeapon.length; ++i) {
            if (boughtWeapon[i]) {
                boolean found = false;
                for (int j = 0; j < 6; ++j) {
                    if (m_Profile.getUsedWeapon()[j] == i) {
                        found = true;
                        break;
                    }
                }
                if (!found) {
                    m_AssembleableWeapon.addElement(WeaponFactory.instance().getWeaponData(i));
                }
            }
        }

        String[] soldWeaponString = new String[m_AssembleableWeapon.size()];
        for (int i = 0; i < soldWeaponString.length; ++i) {
            soldWeaponString[i] = ((WeaponData)m_AssembleableWeapon.elementAt(i)).getName();
        }
        initComp(soldWeaponString);
    }

	public void keyPressed(int key) {
		super.keyPressed(key);

		//Is right button pressed?
		if (key == -7 || key == 35) {
			StateManager.instance().getSFXPlayer().playSFX(SFXPlayer.SFX_CANCEL);
            switch (m_State) {
                case STATE_GARAGE:
                    m_Active = false;
                    break;
                case STATE_BUY_CHOOSE:
                case STATE_SELL_CHOOSE:
                case STATE_ASS_CHOOSE:
                    m_State = STATE_GARAGE;
                    initComp(m_GarageMenuString);
                    break;
                case STATE_BUY_BODY:
                case STATE_BUY_WEAPON:
                    m_State = STATE_BUY_CHOOSE;
                    initComp(m_ChooseMenuString);
                    break;

                case STATE_SELL_BODY:
                case STATE_SELL_WEAPON:
                    m_State = STATE_SELL_CHOOSE;
                    initComp(m_ChooseMenuString);
                    break;
                case STATE_ASS_BODY:
                case STATE_ASS_WEAPON:
                    m_State = STATE_ASS_CHOOSE;
                    initComp(m_ChooseMenuString);
                    break;

				case STATE_BUY_BODY_CONFIRM:
					m_State = STATE_BUY_BODY;
					m_ConfirmIdx = 0;
					for(int i=0; i<m_BtnConfirm.size(); ++i){
						((ButtonPush)m_BtnConfirm.elementAt(i)).setState(Button.STATE_NONE);
					}

					((ButtonPush)m_BtnConfirm.elementAt(m_ConfirmIdx)).setState(Button.STATE_SELECTED);
					break;
				case STATE_BUY_WEAPON_CONFIRM:
					m_State = STATE_BUY_WEAPON;
					m_ConfirmIdx = 0;
					for(int i=0; i<m_BtnConfirm.size(); ++i){
						((ButtonPush)m_BtnConfirm.elementAt(i)).setState(Button.STATE_NONE);
					}

					((ButtonPush)m_BtnConfirm.elementAt(m_ConfirmIdx)).setState(Button.STATE_SELECTED);
					break;
				case STATE_SELL_BODY_CONFIRM:
					m_State = STATE_SELL_BODY;
					m_ConfirmIdx = 0;
					for(int i=0; i<m_BtnConfirm.size(); ++i){
						((ButtonPush)m_BtnConfirm.elementAt(i)).setState(Button.STATE_NONE);
					}

					((ButtonPush)m_BtnConfirm.elementAt(m_ConfirmIdx)).setState(Button.STATE_SELECTED);
					break;
				case STATE_SELL_WEAPON_CONFIRM:
					m_State = STATE_SELL_WEAPON;
					m_ConfirmIdx = 0;
					for(int i=0; i<m_BtnConfirm.size(); ++i){
						((ButtonPush)m_BtnConfirm.elementAt(i)).setState(Button.STATE_NONE);
					}

					((ButtonPush)m_BtnConfirm.elementAt(m_ConfirmIdx)).setState(Button.STATE_SELECTED);
					break;
            }
		}
	}

	public void update(long time, int keys) {
		super.update(time, keys);

		if( ((keys & GameCanvas.FIRE_PRESSED) != 0) ){
			StateManager.instance().getSFXPlayer().playSFX(SFXPlayer.SFX_SELECT);
			if(m_State < STATE_BUY_BODY_CONFIRM && m_Index!=m_Menu.size()-1) //if not on confirmation state AND not on BACK button
				m_IndexSave[m_State] = m_Index;
			
            switch (m_State) {
                case STATE_GARAGE:
                    switch(m_Index){
                        case 0: //Buy clicked
                            m_State = STATE_BUY_CHOOSE;
                            initComp(m_ChooseMenuString);
                            break;
                        case 1: //Sell clicked
                            m_State = STATE_SELL_CHOOSE;
                            initComp(m_ChooseMenuString);
                            break;
                        case 2: //Assembly clicked
                            m_State = STATE_ASS_CHOOSE;
                            initComp(m_ChooseMenuString);
                            break;
                        case 3: //Exit clicked
                            //StateManager.instance().goTo(GameState.MENU, param, false);
                            m_Active = false;
                            break;
                        }
                    break;
                case STATE_BUY_CHOOSE:
                    switch(m_Index){
                        case 0:
                            m_State = STATE_BUY_BODY;
                            initBuyBody();
                            break;
                        case 1:
                            m_State = STATE_BUY_WEAPON;
                            initBuyWeapon();
                            break;
                        case 2:
                            m_State = STATE_GARAGE;
                            initComp(m_GarageMenuString);
                            break;
                        }
                    break;
                case STATE_SELL_CHOOSE:
                    switch(m_Index){
                        case 0:
                            m_State = STATE_SELL_BODY;
                            initSellBody();
                            break;
                        case 1:
                            m_State = STATE_SELL_WEAPON;
                            initSellWeapon();
                            break;
                        case 2:
                            m_State = STATE_GARAGE;
                            initComp(m_GarageMenuString);
                            break;
                        }
                    break;
                case STATE_ASS_CHOOSE:
                    switch(m_Index){
                        case 0:
                            m_State = STATE_ASS_BODY;
                            initAssemblyBody();
                            break;
                        case 1:
                            m_State = STATE_ASS_WEAPON;
                            initAssemblyWeapon();
                            break;
                        case 2:
                            m_State = STATE_GARAGE;
                            initComp(m_GarageMenuString);
                            break;
                        }
                    break;
                case STATE_BUY_BODY:
					if (m_Index == m_Menu.size()-1){
						m_State = STATE_BUY_CHOOSE;
						initComp(m_ChooseMenuString);
					} else if (m_Index < m_BuyableBody.size()) {
						m_State = STATE_BUY_BODY_CONFIRM;
                    }
                    break;
				case STATE_BUY_BODY_CONFIRM:
					if(m_ConfirmIdx == CONFIRM_YES)
						confirmBodyPurchase((BodyData)m_BuyableBody.elementAt(m_Index));
					m_State = STATE_BUY_BODY;
					
					m_ConfirmIdx = 0;
					for(int i=0; i<m_BtnConfirm.size(); ++i){
						((ButtonPush)m_BtnConfirm.elementAt(i)).setState(Button.STATE_NONE);
					}

					((ButtonPush)m_BtnConfirm.elementAt(m_ConfirmIdx)).setState(Button.STATE_SELECTED);
					break;
                case STATE_BUY_WEAPON:
					if (m_Index == m_Menu.size()-1){
						m_State = STATE_BUY_CHOOSE;
						initComp(m_ChooseMenuString);
					} else if (m_Index < m_BuyableWeapon.size()) {
						m_State = STATE_BUY_WEAPON_CONFIRM;
                    }
                    break;
				case STATE_BUY_WEAPON_CONFIRM:
					if(m_ConfirmIdx == CONFIRM_YES)
						confirmWeaponPurchase((WeaponData)m_BuyableWeapon.elementAt(m_Index));
					m_State = STATE_BUY_WEAPON;

					m_ConfirmIdx = 0;
					for(int i=0; i<m_BtnConfirm.size(); ++i){
						((ButtonPush)m_BtnConfirm.elementAt(i)).setState(Button.STATE_NONE);
					}

					((ButtonPush)m_BtnConfirm.elementAt(m_ConfirmIdx)).setState(Button.STATE_SELECTED);
					break;
                case STATE_SELL_BODY:
					if (m_Index == m_Menu.size()-1){
						m_State = STATE_SELL_CHOOSE;
						initComp(m_ChooseMenuString);
					} else if (m_Index < m_SellableBody.size()) {
						m_State = STATE_SELL_BODY_CONFIRM;
                    }
                    break;
				case STATE_SELL_BODY_CONFIRM:
					if(m_ConfirmIdx == CONFIRM_YES)
						confirmBodySelling((BodyData)m_BuyableBody.elementAt(m_Index));
					m_State = STATE_SELL_BODY;

					m_ConfirmIdx = 0;
					for(int i=0; i<m_BtnConfirm.size(); ++i){
						((ButtonPush)m_BtnConfirm.elementAt(i)).setState(Button.STATE_NONE);
					}

					((ButtonPush)m_BtnConfirm.elementAt(m_ConfirmIdx)).setState(Button.STATE_SELECTED);
					break;
                case STATE_SELL_WEAPON:
					if (m_Index == m_Menu.size()-1){
						m_State = STATE_SELL_CHOOSE;
						initComp(m_ChooseMenuString);
					} else if (m_Index < m_SellableWeapon.size()) {
						m_State = STATE_SELL_WEAPON_CONFIRM;
                    }
                    break;
				case STATE_SELL_WEAPON_CONFIRM:
					if(m_ConfirmIdx == CONFIRM_YES)
						confirmWeaponSelling((WeaponData)m_BuyableWeapon.elementAt(m_Index));
					m_State = STATE_SELL_WEAPON;

					m_ConfirmIdx = 0;
					for(int i=0; i<m_BtnConfirm.size(); ++i){
						((ButtonPush)m_BtnConfirm.elementAt(i)).setState(Button.STATE_NONE);
					}

					((ButtonPush)m_BtnConfirm.elementAt(m_ConfirmIdx)).setState(Button.STATE_SELECTED);
					break;
                case STATE_ASS_BODY:
					if (m_Index == m_Menu.size()-1){
						m_State = STATE_ASS_CHOOSE;
						initComp(m_ChooseMenuString);
					} else if (m_Index < m_AssembleableBody.size()) {
                        final BodyData data = (BodyData)m_AssembleableBody.elementAt(m_Index);
                        m_Profile.setUsedBody(data.getIndex());
                        initAssemblyBody();
                    }
                    break;
                case STATE_ASS_WEAPON:
					if (m_Index == m_Menu.size()-1){
						m_State = STATE_ASS_CHOOSE;
						initComp(m_ChooseMenuString);
					} else if (m_Index < m_AssembleableWeapon.size()) {
                        final WeaponData data = (WeaponData)m_AssembleableWeapon.elementAt(m_Index);
                        if (data.getWeaponType() != WeaponData.WEAPON_TYPE_CIRCULAR) {
                            m_Profile.getUsedWeapon()[data.getWeaponType()] = data.getIndex();
                        } else {
                            boolean found = false;
                            for (int i = 3; i < 6; ++i) {
                                if (m_Profile.getUsedWeapon()[i] == -1) {
                                    m_Profile.getUsedWeapon()[i] = data.getIndex();
                                    found = true;
                                    break;
                                }
                            }
                            if (!found) {
                                for (int i = 4; i < 6; ++i) {
                                    m_Profile.getUsedWeapon()[i - 1] = m_Profile.getUsedWeapon()[i];
                                }
                                m_Profile.getUsedWeapon()[5] = data.getIndex();
                            }
                        }
                        initAssemblyWeapon();
                    }
                    break;
            }
		}

        // remove weapon based on selection if cursor is moving
        if ((keys & GameCanvas.UP_PRESSED) != 0 || (keys & GameCanvas.DOWN_PRESSED) != 0) {
			if(m_Index < m_Menu.size()-1){ //-1 because the last one is always BACK button
				switch (m_State) {
					case STATE_ASS_WEAPON:
						m_CurrentPlayer.removeWeaponWithData((WeaponData)m_AssembleableWeapon.elementAt(m_Index));
						break;
					case STATE_BUY_WEAPON:
						m_CurrentPlayer.removeWeaponWithData((WeaponData)m_BuyableWeapon.elementAt(m_Index));
						break;
					case STATE_SELL_WEAPON:
						m_CurrentPlayer.removeWeaponWithData((WeaponData)m_SellableWeapon.elementAt(m_Index));
						break;
				}
			}

            //Move confirmation index only
			if(m_State == STATE_BUY_BODY_CONFIRM || m_State == STATE_BUY_WEAPON_CONFIRM || m_State == STATE_SELL_BODY_CONFIRM || m_State == STATE_SELL_WEAPON_CONFIRM){
				if( (keys & GameCanvas.UP_PRESSED) != 0)	m_ConfirmIdx--;
				else										m_ConfirmIdx++;

				if(m_ConfirmIdx < 0)						m_ConfirmIdx = m_BtnConfirm.size()-1;
				else if(m_ConfirmIdx >= m_BtnConfirm.size())m_ConfirmIdx = 0;

				for(int i=0; i<m_BtnConfirm.size(); ++i){
					((ButtonPush)m_BtnConfirm.elementAt(i)).setState(Button.STATE_NONE);
				}

				((ButtonPush)m_BtnConfirm.elementAt(m_ConfirmIdx)).setState(Button.STATE_SELECTED);
			} else { // move normal index
				m_Menu.update(keys);
				if(m_Menu.size() > 0)
					m_Index = m_Menu.getSelectedButton().getID();

				// update menu drawing
				if(m_Menu != null && m_Menu.size() > 0) {
					if (m_Index < m_Menu.size()-1) { //-1 because the last one is always BACK button
						// set weapon based on selection
						Object temp = null;
						switch (m_State) {
							case STATE_ASS_BODY:
								m_CurrentPlayer.setData((BodyData)m_AssembleableBody.elementAt(m_Index));
								temp		= m_AssembleableBody.elementAt(m_Index);
								m_Price		= BitmapFont.getFont(BitmapFont.MAIN).renderTransparentText("Price: " + ((BodyData)temp).getPrice(), 0x000000);
								m_Damage	= BitmapFont.getFont(BitmapFont.MAIN).renderTransparentText("Damage: -", 0x000000);
								m_Seeking	= BitmapFont.getFont(BitmapFont.MAIN).renderTransparentText("Seeking: No", 0x000000);
								if (((BodyData)temp).getDescription() != null) m_Desc = BitmapFont.getFont(BitmapFont.MAIN).renderTransparentText(((BodyData)temp).getDescription(), 0x000000);
								break;
							case STATE_BUY_BODY:
								m_CurrentPlayer.setData((BodyData) m_BuyableBody.elementAt(m_Index));
								temp		= m_BuyableBody.elementAt(m_Index);
								m_Price		= BitmapFont.getFont(BitmapFont.MAIN).renderTransparentText("Price: " + ((BodyData)temp).getPrice(), 0x000000);
								m_Damage	= BitmapFont.getFont(BitmapFont.MAIN).renderTransparentText("Damage: -", 0x000000);
								m_Seeking	= BitmapFont.getFont(BitmapFont.MAIN).renderTransparentText("Seeking: No", 0x000000);
								if (((BodyData)temp).getDescription() != null) m_Desc= BitmapFont.getFont(BitmapFont.MAIN).renderTransparentText(((BodyData)temp).getDescription(), 0x000000);
								break;
							case STATE_SELL_BODY:
								m_CurrentPlayer.setData((BodyData) m_SellableBody.elementAt(m_Index));
								temp		= m_SellableBody.elementAt(m_Index);
								m_Price		= BitmapFont.getFont(BitmapFont.MAIN).renderTransparentText("Price: " + ((BodyData)temp).getPrice(), 0x000000);
								m_Damage	= BitmapFont.getFont(BitmapFont.MAIN).renderTransparentText("Damage: -", 0x000000);
								m_Seeking	= BitmapFont.getFont(BitmapFont.MAIN).renderTransparentText("Seeking: No", 0x000000);
								if (((BodyData)temp).getDescription() != null) m_Desc = BitmapFont.getFont(BitmapFont.MAIN).renderTransparentText(((BodyData)temp).getDescription(), 0x000000);
								break;
							case STATE_ASS_WEAPON:
								m_CurrentPlayer.addWeapon(new Weapon((WeaponData) m_AssembleableWeapon.elementAt(m_Index)));
								temp		= m_AssembleableWeapon.elementAt(m_Index);
								m_Price		= BitmapFont.getFont(BitmapFont.MAIN).renderTransparentText("Price: " + ((WeaponData)temp).getPrice(), 0x000000);
								m_Damage	= BitmapFont.getFont(BitmapFont.MAIN).renderTransparentText("Damage: " + ((WeaponData)temp).getBulletData().getDamage(), 0x000000);
								m_Seeking	= BitmapFont.getFont(BitmapFont.MAIN).renderTransparentText("Seeking: " + (((WeaponData)temp).isSeeking() == true ? "Yes" : "No"), 0x000000);
								if (((WeaponData)temp).getDescription() != null) m_Desc = BitmapFont.getFont(BitmapFont.MAIN).renderTransparentText(((WeaponData)temp).getDescription(), 0x000000);
								break;
							case STATE_BUY_WEAPON:
								m_CurrentPlayer.addWeapon(new Weapon((WeaponData) m_BuyableWeapon.elementAt(m_Index)));
								temp		= m_BuyableWeapon.elementAt(m_Index);
								m_Price		= BitmapFont.getFont(BitmapFont.MAIN).renderTransparentText("Price: " + ((WeaponData)temp).getPrice(), 0x000000);
								m_Damage	= BitmapFont.getFont(BitmapFont.MAIN).renderTransparentText("Damage: " + ((WeaponData)temp).getBulletData().getDamage(), 0x000000);
								m_Seeking	= BitmapFont.getFont(BitmapFont.MAIN).renderTransparentText("Seeking: " + (((WeaponData)temp).isSeeking() == true ? "Yes" : "No"), 0x000000);
								if (((WeaponData)temp).getDescription() != null) m_Desc = BitmapFont.getFont(BitmapFont.MAIN).renderTransparentText(((WeaponData)temp).getDescription(), 0x000000);
								break;
							case STATE_SELL_WEAPON:
								m_CurrentPlayer.addWeapon(new Weapon((WeaponData) m_SellableWeapon.elementAt(m_Index)));
								temp		= m_SellableWeapon.elementAt(m_Index);
								m_Price		= BitmapFont.getFont(BitmapFont.MAIN).renderTransparentText("Price: " + ((WeaponData)temp).getPrice(), 0x000000);
								m_Damage	= BitmapFont.getFont(BitmapFont.MAIN).renderTransparentText("Damage: " + ((WeaponData)temp).getBulletData().getDamage(), 0x000000);
								m_Seeking	= BitmapFont.getFont(BitmapFont.MAIN).renderTransparentText("Seeking: " + (((WeaponData)temp).isSeeking() == true ? "Yes" : "No"), 0x000000);
								if (((WeaponData)temp).getDescription() != null) m_Desc = BitmapFont.getFont(BitmapFont.MAIN).renderTransparentText(((WeaponData)temp).getDescription(), 0x000000);
								break;
						}
					}
				}
			}

			//play sfx
            StateManager.instance().getSFXPlayer().playSFX(SFXPlayer.SFX_CURSOR);
        }

        switch (m_State) {
            case STATE_ASS_BODY:
            case STATE_ASS_WEAPON:
            case STATE_BUY_BODY:
            case STATE_BUY_WEAPON:
            case STATE_SELL_BODY:
            case STATE_SELL_WEAPON:
                m_CurrentPlayer.update(time);
                break;
            default:
                m_Player.update(time);
                break;
        }

		m_Menu.forceDraw();
	}

	public void draw(Graphics g) {
		super.draw(g);
		
		g.drawImage(StateManager.instance().getDarkImage(), 0, 0, Graphics.LEFT | Graphics.TOP);
		if(m_Menu == null) return;

		m_Menu.draw(g);
        g.drawImage(m_MoneyStringImage, StateManager.instance().getWidth() - 5, 5, Graphics.TOP | Graphics.RIGHT);

		//Draw body/weapon information
		if( (m_State == STATE_ASS_BODY || m_State == STATE_BUY_BODY || m_State == STATE_SELL_BODY || m_State == STATE_ASS_WEAPON || m_State == STATE_BUY_WEAPON || m_State == STATE_SELL_WEAPON) && (m_Index < m_Menu.size()-1) ){
			if(m_Price != null)		g.drawImage(m_Price, 5, 180, Graphics.LEFT | Graphics.TOP);
			if(m_Damage != null)	g.drawImage(m_Damage, 5, 180+15, Graphics.LEFT | Graphics.TOP);
			if(m_Seeking != null)	g.drawImage(m_Seeking, 5, 180+30, Graphics.LEFT | Graphics.TOP);
			if(m_Desc != null)		g.drawImage(m_Desc, StateManager.instance().getWidth()-5, 180, Graphics.RIGHT | Graphics.TOP);
		}

		//Draw the confirmation button only if the state is appropriate
		if(m_State == STATE_BUY_BODY_CONFIRM || m_State == STATE_BUY_WEAPON_CONFIRM || m_State == STATE_SELL_BODY_CONFIRM || m_State == STATE_SELL_WEAPON_CONFIRM){
			g.drawImage(m_ConfirmMessage, 45, StateManager.instance().getHeight()/2 - 20, Graphics.LEFT | Graphics.VCENTER);
			g.drawImage(m_ConfirmPanel, 5, StateManager.instance().getHeight()/2, Graphics.LEFT | Graphics.VCENTER);

			for(int i=0; i<m_BtnConfirm.size(); ++i){
				((ButtonPush)m_BtnConfirm.elementAt(i)).draw(g);
			}
		} else { //Draw the ship
			switch (m_State) {
				case STATE_ASS_BODY:
				case STATE_ASS_WEAPON:
				case STATE_BUY_BODY:
				case STATE_BUY_WEAPON:
				case STATE_SELL_BODY:
				case STATE_SELL_WEAPON:
					m_CurrentPlayer.draw(g, 0, 0, StateManager.instance().getWidth(), StateManager.instance().getHeight());
					break;
				default:
					m_Player.draw(g, 0, 0, StateManager.instance().getWidth(), StateManager.instance().getHeight());
					break;
			}
		}
	}

	//Data
	protected Profile		m_Profile;
	protected Player		m_Player, m_CurrentPlayer;
	protected int			m_Index, m_IndexSave[];		//to save the index position on state which has been visited
    protected Image         m_MoneyStringImage;

    // buy, sell, assembly data
    protected Vector        m_BuyableBody, m_BuyableWeapon;
    protected Vector        m_SellableBody, m_SellableWeapon;
    protected Vector        m_AssembleableBody, m_AssembleableWeapon;

	//Confirmation
	protected int			m_ConfirmIdx;
	protected Image			m_ConfirmPanel, m_ConfirmMessage;
	protected Vector		m_BtnConfirm;

	//Body & weapon data
	protected Image			m_Price, m_Damage, m_Seeking, m_Desc;

    // state
    protected int           m_State;
	protected ButtonWheel	m_Menu;
	protected String[]		m_GarageMenuString = {"Buy", "Sell", "Assemble"};
    protected String[]      m_ChooseMenuString = {"Body", "Weapon"};
	protected String[]		m_ConfirmationString = {"No", "Yes"};

    private final static int STATE_GARAGE       = 0;
    private final static int STATE_BUY_CHOOSE   = 1;
    private final static int STATE_BUY_BODY     = 2;
    private final static int STATE_BUY_WEAPON   = 3;
    private final static int STATE_SELL_CHOOSE  = 4;
    private final static int STATE_SELL_BODY    = 5;
    private final static int STATE_SELL_WEAPON  = 6;
    private final static int STATE_ASS_CHOOSE   = 7;
    private final static int STATE_ASS_BODY     = 8;
    private final static int STATE_ASS_WEAPON   = 9;

	private final static int STATE_BUY_BODY_CONFIRM		= 10;
	private final static int STATE_BUY_WEAPON_CONFIRM	= 11;
	private final static int STATE_SELL_BODY_CONFIRM	= 12;
	private final static int STATE_SELL_WEAPON_CONFIRM	= 13;

	private final static int CONFIRM_NO		= 0;
	private final static int CONFIRM_YES	= 1;
}
